package org.geektimes.projects.spring.cloud.config.server.custom;

import org.apache.tomcat.util.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.util.Collections;
import java.util.Properties;

/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2021/6/3 6:38
 */
@Order(-1)
public class FilePropertySourceLoacator implements PropertySourceLocator {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private String filePath;


    @Override
    public PropertySource<?> locate(Environment environment) {

        Properties properties = getProperties();



        return new MapPropertySource("custom-property",
                Collections.<String, Object>singletonMap("property.from.sample.custom.source", "worked as intended"));
    }



    public Properties getProperties(){
        Properties properties = null;
        try {
            properties = PropertiesLoaderUtils.loadAllProperties(filePath);
        }catch (Exception e){
            logger.error("通过 {}  获取Properties 失败", filePath);
        }
        return properties;
    }



}
